package com.kakavr.shop.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.IShopTypeService;
import com.kakavr.util.ValidatorUtils;
/**
 * 店铺分类管理
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("shopType")
public class ShopTypeController extends BaseController {
	@Resource
	private IShopTypeService shopTypeService;
	
	/**
	 * 添加 店铺分类
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request){
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "名称");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("name", name);
		shopTypeService.addShopType(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 修改店铺分类
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request){
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "id");
		}
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "名称");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("name", name);
		map.put("id", id);
		shopTypeService.updateShopType(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 店铺分类列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectList", method = RequestMethod.GET)
	public Map<String, Object> selectList(HttpServletRequest request){
		return MsgResult.okResultData(shopTypeService.selectShopTypeList());
	}

}
